<?php
/**
 *
 *
 */
namespace Yz\modules\Generic;

use Yz_Array;

class Controller_Edit
    extends Controller_AbstractUpdate
{

    /**
     *  forbid access beyond admin panel
     *  @const int
     */
    const ACCESS = \Yz_Access_Interface::AC_ADMIN;

    /** allow presence in automatic menus
     *  @const ???
     */
    //const IN_MENU = true;

    /** The list of required arguments for controller
     *  @var array
     */
    protected $arguments = array(
        'itemId',
    );


    /**
     *  @var string|null (null -> Default model)
     */
    protected $modelName = null;

    /**
     *  @var string|null (null -> Default form)
     */
    protected $formName = null;


    protected $model;
    protected $item;
    protected $itemId;
    protected $form;


    /******************************
     *     Protected methods      *
     ******************************/


    /** parse arguments: determine item-id
     *  @param string(int) arg (from URI)
     *  @return this
     *  @throw Yz_Controller_Exception_InvalidArgument
     */
    protected function setItemId($arg)
    {
        $this->itemId = \Yz_Int::testPositive($arg);
        return $this;
    }


    /** fetch data: load item by this.itemId
     *  @return this
     */
    protected function fetchData()
    {
        debug();
        $this->item = $this->fetchItem();
        $this->form = $this->createForm($this->item);
        return $this;
    }


    /** process data: parse post arguments using the form
     *  @return this
     */
    protected function processData(\Yz_Hash $post)
    {
        debug();
        try {
            $this->form->parse($post);
            $this->item->assignValues($this->form->getValues());
        } catch (\Yz_Form_Parser_Exception $ex) {
            $this->error = $ex;
            debug($ex->getTraceAsString());
        }
        return $this;
    }


    /**
     *  @return this
     *
     */
    protected function updateModel()
    {
        debug();
        if (null === $this->error) {
            #try {
                $this->selectModel()->updateItemById($this->item);
                $this->success = true;
            #} catch (\Yz_SQL_Exception_Unique $ex) {
            #    $this->error = $ex;
            #}
        }
        return $this;
    }


    /**
     *  @return this
     *
     */
    protected function assignTemplateParams(\Yz_Template_Interface $t)
    {
        $t->item = $this->item;
        $t->itemId = $this->itemId;
        $t->includeForm($this->form);
        return parent::assignTemplateParams($t);
    }

    /**
     *
     */
    protected function createForm($item)
    {
        debug();
        $formName = $this->formName;
        if (null === $formName) {
            $formName = 'Default';
        }
        $formClass = $this->module->resolveLibrary('Form', $formName);
        $form = new $formClass( $this->module, $item->describe() );
        $form->assignValues($item);
        return $form;
    }


    /** fetch/initialize item
     *  @return Yz_Item_Interface
     */
    protected function fetchItem()
    {
        return $this->selectModel()->fetchItemById($this->itemId);
    }


}
